package com.lc.cehui.ui.activity.home;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.cehui.R;
import com.lc.cehui.app.AppActivity;
import com.lc.cehui.http.glide.GlideApp;
import com.lc.cehui.http.request.CompanyJobApi;
import com.lc.cehui.http.response.CompanyJobBean;
import com.lc.cehui.ui.activity.GoodsDetailsActivity;
import com.lc.cehui.ui.activity.home.search.ExhibitionActivity;
import com.lc.cehui.widget.XCollapsingToolbarLayout;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 资源共享
 */
public class IntelligenceActivity extends AppActivity implements XCollapsingToolbarLayout.OnScrimsListener {
    @BindView(R.id.kind_recy)
    RecyclerView kindRecy;
    @BindView(R.id.tb_home_title)
    Toolbar tbHomeTitle;
    @BindView(R.id.ctl_home_bar)
    XCollapsingToolbarLayout mCollapsingToolbarLayout;
    @BindView(R.id.rv_mingqi_bottom)
    RecyclerView rvMingqiBottom;
    @BindView(R.id.img_logo)
    ImageView imgLogo;
    private List<CompanyJobBean.DataBean.GoodsBean>  dataGoodsList = new ArrayList<>();
    private List<CompanyJobBean.DataBean.TypeBean> kindmiddles = new ArrayList<>();
    private BaseQuickAdapter<CompanyJobBean.DataBean.GoodsBean,BaseViewHolder> mingqibottomadpter;
    private BaseQuickAdapter kindadapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_intelligence;
    }

    @Override
    protected void initView() {
        company_job();


        kindRecy.setLayoutManager(new GridLayoutManager(getActivity(), 5));
        kindadapter = new BaseQuickAdapter<CompanyJobBean.DataBean.TypeBean, BaseViewHolder>(R.layout.item_qiye_top, kindmiddles) {
            @Override
            protected void convert(@NotNull BaseViewHolder baseViewHolder, CompanyJobBean.DataBean.TypeBean bean) {
                baseViewHolder.setText(R.id.tv_menu_title_home, bean.getTitle());
                GlideApp.with(IntelligenceActivity.this).load(bean.getPicurl()).apply(new RequestOptions().placeholder(R.drawable.image11).error(R.drawable.image11)).into((ImageView) baseViewHolder.getView(R.id.iv_menu_home));

            }
        };
        kindRecy.setAdapter(kindadapter);
        kindadapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
                switch (kindmiddles.get(position).getId()) {
                    case 46:
//                   展会信息

                        startActivity(new Intent(IntelligenceActivity.this,ExhibitionActivity.class).putExtra("title",kindmiddles.get(position).getTitle()).putExtra("id",kindmiddles.get(position).getId()+""));
                        break;
                    case 47:

//                  产品发布
                        startActivity(new Intent(IntelligenceActivity.this,ExhibitionActivity.class).putExtra("title",kindmiddles.get(position).getTitle()).putExtra("id",kindmiddles.get(position).getId()+""));
                        break;
                    case 48:
//                         资源共享
                        startActivity(new Intent(IntelligenceActivity.this,IntelligenceSecondActivity.class).putExtra("title",kindmiddles.get(position).getTitle()).putExtra("id",kindmiddles.get(position).getId()+""));

                        break;
                    case 49:
//                    招标信息
                        startActivity(new Intent(IntelligenceActivity.this,IntelligenceSecondActivity.class).putExtra("title",kindmiddles.get(position).getTitle()).putExtra("id",kindmiddles.get(position).getId()+""));
                        break;
                    case 50:
//                   干货分享
                        startActivity(new Intent(IntelligenceActivity.this,IntelligenceSecondActivity.class).putExtra("title",kindmiddles.get(position).getTitle()).putExtra("id",kindmiddles.get(position).getId()+""));
                        break;

                }

            }
        });
        rvMingqiBottom.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        mingqibottomadpter = new BaseQuickAdapter<CompanyJobBean.DataBean.GoodsBean, BaseViewHolder>(R.layout.item_index_goods) {
            @Override
            protected void convert(@NotNull BaseViewHolder baseViewHolder, CompanyJobBean.DataBean.GoodsBean bean) {
                baseViewHolder.setText(R.id.title_name, bean.getName());
                GlideApp.with(IntelligenceActivity.this).load(bean.getPicurl()).apply(new RequestOptions().placeholder(R.drawable.image11).error(R.drawable.image11)).into((ImageView) baseViewHolder.getView(R.id.goods_img));

                RecyclerView recyclerView=baseViewHolder.getView(R.id.rv_coupon);
                LinearLayoutManager lm = new LinearLayoutManager(getContext());
                lm.setOrientation(LinearLayoutManager.HORIZONTAL);
                recyclerView.setLayoutManager(lm);
                BaseQuickAdapter<CompanyJobBean.DataBean.GoodsBean.CouponBean,BaseViewHolder> mAdapter=new BaseQuickAdapter<CompanyJobBean.DataBean.GoodsBean.CouponBean, BaseViewHolder>(R.layout.item_index_coupon,bean.getCoupon()) {
                    @Override
                    protected void convert(@NotNull BaseViewHolder holder, CompanyJobBean.DataBean.GoodsBean.CouponBean couponBean) {
                        holder.setText(R.id.tv_coupon,"满"+couponBean.getShold_price()+"减"+couponBean.getRed_price());
                    }
                };
                recyclerView.setAdapter(mAdapter);
                baseViewHolder.setText(R.id.tv_preprice, bean.getY_price());
                TextView price=baseViewHolder.getView(R.id.tv_price);
                price.setText("¥"+bean.getPrice());
                price.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG);
            }
        };
        rvMingqiBottom.setAdapter(mingqibottomadpter);
        mingqibottomadpter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                startActivity(new Intent(getContext(), GoodsDetailsActivity.class).putExtra("id",mingqibottomadpter.getData().get(position).getGoods_id()));
            }
        });
        //设置渐变监听
        mCollapsingToolbarLayout.setOnScrimsListener(this);
    }

    @Override
    protected void initData() {

    }

    private void company_job() {
        EasyHttp.post(this)
                .api(new CompanyJobApi().setId(getIntent().getStringExtra("id")))
                .request(new HttpCallback<CompanyJobBean>(this) {

                    @Override
                    public void onSucceed(CompanyJobBean data) {

                        if (data.getCode().equals("200")) {
                            GlideApp.with(IntelligenceActivity.this).load(data.getData().getImages()).apply(new RequestOptions().placeholder(R.mipmap.logo_qyzp).error(R.mipmap.logo_qyzp)).into((ImageView) imgLogo);

                                    kindmiddles.addAll(data.getData().getType());


//
                            dataGoodsList.addAll(data.getData().getGoods());
                            kindadapter.setNewData(kindmiddles);
                            kindadapter.notifyDataSetChanged();
//
//
                            mingqibottomadpter.setNewData(dataGoodsList);
                            mingqibottomadpter.notifyDataSetChanged();
                        }

                    }
                });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.img_back, R.id.ll_seach})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.ll_seach:
                break;
        }
    }

    @Override
    public void onScrimsStateChange(XCollapsingToolbarLayout layout, boolean shown) {
        if (shown) {
            tbHomeTitle.setBackgroundResource(R.color.main_color);
//
        } else {
            tbHomeTitle.setBackgroundResource(R.color.black100);

//
        }
    }
}
